package com.mikhailenko.transportMVC.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mikhailenko.transportMVC.model.Criteria;
import com.mikhailenko.transportMVC.model.Firm;
import com.mikhailenko.transportMVC.model.OperationEnum;
import com.mikhailenko.transportMVC.model.Table;
import com.mikhailenko.transportMVC.model.TableForm;
import com.mikhailenko.transportMVC.service.CriteriaService;
import com.mikhailenko.transportMVC.service.FirmService;
import com.mikhailenko.transportMVC.utils.Convertor;
import com.mikhailenko.transportMVC.utils.Nmai;
import com.mikhailenko.transportMVC.utils.TableUtil;
import com.mikhailenko.transportMVC.validator.FuzzyTableValidator;

@Controller
@RequestMapping(value="/table")
public class TableController {
	
	@Autowired
	private Nmai nmai;
	
	@Autowired
	private FirmService firmService;
	
	@Autowired
	private FuzzyTableValidator tableValidator;
	
	@Autowired
	private TableUtil tableUtil;
	
	@Autowired
	private CriteriaService criteriaService;
	
	@InitBinder("tableForm")
	private void initBinder(WebDataBinder binder) {
		binder.setValidator(tableValidator);
	}
	
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView newTablePage() {
		ModelAndView mav = new ModelAndView("table", "tableForm", new TableForm());
		
		List<Firm> firms = firmService.findAll();
		mav.addObject("firms", firms);
		
		List<Criteria> criteria = criteriaService.findAll();
		mav.addObject("criteria", criteria);
		
		return mav;
	}
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createNewTable( @ModelAttribute @Valid TableForm tableForm,
			BindingResult result,
			final RedirectAttributes redirectAttributes) {
		
		if (result.hasErrors()){
			ModelAndView mav = new ModelAndView("table");
			
			List<Firm> firms = firmService.findAll();
			mav.addObject("firms", firms);
			
			List<Criteria> criteria = criteriaService.findAll();
	        mav.addObject("criteria", criteria);
			
			return mav;
		}
		
		ModelAndView mav = new ModelAndView();

		for (int i = 0; i < tableForm.getTableList().size(); i++){
		    Table table = tableForm.getTableList().get(i);
	        
	        List<String> rowSum = new ArrayList<String>();
	        String detetminedVector = null;
	        List<String> multiplication = new ArrayList<String>();
	        List<String> priorityVector = new ArrayList<String>();
	        List<List<String>> desicionTable =  tableUtil.fill(table.getTable());
	        table.setTable(desicionTable);
	        
	        if(table.getOperation().equals(OperationEnum.EXTENT_ANALYSIS)){
	            double[][] rowS = nmai.getRowSum(desicionTable);
	            rowSum = Convertor.triangleMasToList(rowS);
	            table.setRowSum(rowSum);
	            
	            double[] detVector = nmai.getDeterminedVectorInverce(desicionTable); 
	            detetminedVector = Convertor.singleMasToString(detVector);
	            table.setDetetminedVector(detetminedVector);
	            
	            double[][] mul = nmai.getFuzzyMultiplication(rowS, detVector); 
	            multiplication = Convertor.triangleMasToList(mul);
	            table.setMultiplication(multiplication);
	            
	            double[] vector = nmai.getPriorityVector(mul);
	            priorityVector = Convertor.masToList(vector);
	            table.setPriorityVector(priorityVector);
	            
	        } else {
	            //TODO: need implement other methods
	        }
		}
		
		mav.setViewName("redirect:/table/result");
		
		Table mainResult = nmai.getResultTable(tableForm);
		
		redirectAttributes.addFlashAttribute("tableResult", tableForm);
		redirectAttributes.addFlashAttribute("mainResult", mainResult);

		return mav;
	}
	
	@RequestMapping(value = "/result", method = RequestMethod.GET)
	public ModelAndView newResultPage(@ModelAttribute("tableResult") final TableForm tableForm,
	        @ModelAttribute("mainResult") final Table mainResult) {
		
		ModelAndView mav = new ModelAndView("table-result", "tableResult", tableForm);

		List<Firm> firms = firmService.findAll();
		mav.addObject("firms", firms);
		
		List<Criteria> criteria = criteriaService.findAll();
        mav.addObject("criteria", criteria);

        mav.addObject("mainResult", mainResult);
		return mav;
	}
	
	@RequestMapping(value = "/hierarchy", method=RequestMethod.GET)
	public ModelAndView newHierarchPage(){
	    ModelAndView mav = new ModelAndView("table-hierarchy");
	    
	    List<Firm> firms = firmService.findAll();
        mav.addObject("firms", firms);
        
        List<Criteria> criteria = criteriaService.findAll();
        mav.addObject("criteria", criteria);
        
	    return mav;
	}

}
